<?php

namespace UF\AccountBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use UF\AccountBundle\Entity\Account;
use UF\AccountBundle\Form\AccountType;
use UF\AccountBundle\Form\AccountHandler;

class AccountController extends Controller
{
    
    public function listAccountsAction()
    {
    	//Get name of connected account
    	$session=$this->getRequest()->getSession();
    	$user=$this->get('security.context')->getToken()->getUser();
    	$name = $user->getUsername();
    	
    	//Get list of saved accounts
    	$em = $this->getDoctrine()->getRepository('UFAccountBundle:Account');
    	$accounts = $em->findAll();
    	
    	$params = array("name" => $name, 
    				"accounts" => $accounts);
    	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
        return $this->render('UFAccountBundle:Accounts:accountsList.html.twig', $params);
    }
    
    public function addAccountAction()
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	$repo = $this->getDoctrine()->getRepository('UFAccountBundle:Account');

    	$account = new Account();
    	$accountForm = $this->createForm(new AccountType(), $account);  		
    	$accountHandler = new AccountHandler($accountForm, $request, $em);
    	
    	try{
	    	if($accountHandler->process()){
	    		return $this->redirect($this->generateUrl("UFAccountBundle_listAccounts"));
	    	}
    	}
    	catch(\Exception $e){throw $this->createNotFoundException("Le compte n'a pas pu être ajouté");}
    	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Accounts:addAccount.html.twig', array("accountForm" => $accountForm->createView()));
    }
    
    public function modifyAccountAction($accountId)
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	$repo = $this->getDoctrine()->getRepository('UFAccountBundle:Account');

    	$account = $repo->find($accountId);
    	$accountForm = $this->createForm(new AccountType(), $account);
    	$accountHandler = new AccountHandler($accountForm, $request, $em);

    	try{
    		if($accountHandler->process()){
    			return $this->redirect($this->generateUrl("UFAccountBundle_listAccounts"));
    		}
    	}
    	catch(\Exception $e){
    		throw $this->createNotFoundException("Le compte n'a pas pu être modifié");
    	}
    	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Accounts:modifyAccount.html.twig', array("accountForm" => $accountForm->createView(), "accountId" => $accountId));
    	
    }
    
    
    public function deleteAccountAction($accountId)
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	$repo = $this->getDoctrine()->getRepository('UFAccountBundle:Account');
    	 
    	try
    	{
    		$account = $repo->find($accountId);
    		$em->remove($account);
    		$em->flush();
    	}
    	catch (\Exception $e)
    	{
    		throw $this->createNotFoundException("Le compte n'a pas pu être supprimé");
    	}
    	 
    	return $this->redirect($this->generateUrl("UFAccountBundle_listAccounts"));
    }
    
}
